package ${baseInfo.packageName};

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${request.packageName}.ApiResult;
import ${tableClass.fullClassName};
import ${serviceInterface.packageName}.${serviceInterface.fileName};
import ${request.packageName}.Request${tableClass.shortClassName};
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;
import java.util.stream.Stream;

@Slf4j
@RestController
@RequestMapping("/${tableClass.shortClassName?uncap_first}")
@Api(value = "${tableClass.shortClassName}API", tags = {"${tableClass.shortClassName}"})
public class ${tableClass.shortClassName}Controller {
    @Autowired
    private ${tableClass.shortClassName}Service ${tableClass.shortClassName?uncap_first}Service;

    /**
    * 添加
    */
    @PostMapping("/add")
    @ApiOperation(value = "添加", response = ApiResult.class)
    public ApiResult<Boolean> add(@RequestBody ${tableClass.shortClassName} ${tableClass.shortClassName?uncap_first}) throws Exception {
        boolean flag = ${tableClass.shortClassName?uncap_first}Service.save(${tableClass.shortClassName?uncap_first});
        return ApiResult.result(flag);
    }

    /**
    * 修改
    */
    @PostMapping("/update")
    @ApiOperation(value = "修改", response = ApiResult.class)
    public ApiResult<Boolean> update(@RequestBody ${tableClass.shortClassName} ${tableClass.shortClassName?uncap_first}) throws Exception {
        boolean flag = ${tableClass.shortClassName?uncap_first}Service.saveOrUpdate(${tableClass.shortClassName?uncap_first});
        return ApiResult.result(flag);
    }

    /**
    * 删除
    */
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "删除", response = ApiResult.class)
    public ApiResult<Boolean> delete(@PathVariable("id") Long id) throws Exception {
        boolean flag = ${tableClass.shortClassName?uncap_first}Service.removeById(id);
        return ApiResult.result(flag);
    }

    /**
    * 批量删除
    * @param ids
    * @return
    * @throws Exception
    */
    @RequestMapping("/delByIds")
    @ApiOperation(value = "批量删除", response = ApiResult.class)
    public ApiResult<Boolean> delByIds(String ids) throws Exception {
        if (Objects.isNull(ids)) {
            return ApiResult.ok();
        }
        Stream.of(ids.split(",")).forEach(id -> {
            ${tableClass.shortClassName?uncap_first}Service.removeById(Long.valueOf(id));
        });
        return ApiResult.ok();
    }

    /**
    * 获取详情
    */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "用户", response = ${tableClass.shortClassName}.class)
    public ApiResult<${tableClass.shortClassName}> get${tableClass.shortClassName}(@PathVariable("id") Long id) throws Exception {
        ${tableClass.shortClassName} ${tableClass.shortClassName?uncap_first} = ${tableClass.shortClassName?uncap_first}Service.getById(id);
        return ApiResult.ok(${tableClass.shortClassName?uncap_first});
    }

    /**
    * 分页列表
    */
    @PostMapping("/getPageList")
    @ApiOperation(value = "分页列表", response = ${tableClass.shortClassName}.class)
    public ApiResult<IPage<${tableClass.shortClassName}>> get${tableClass.shortClassName}PageList(@RequestBody Request${tableClass.shortClassName} query${tableClass.shortClassName}) throws Exception {
        Page<${tableClass.shortClassName}> queryPage = Page.of(query${tableClass.shortClassName}.getPage(), query${tableClass.shortClassName}.getLimit());
        Wrapper<${tableClass.shortClassName}> queryWapper = new QueryWrapper<>();
        IPage<${tableClass.shortClassName}> page = ${tableClass.shortClassName?uncap_first}Service.page(queryPage,queryWapper);
        return ApiResult.ok(page);
    }
}